package com.group5.controller;

import com.group5.dao.PlatformMapper;
import com.group5.dao.UserssMapper;
import com.group5.pojo.Platform;
import com.group5.pojo.Userss;
import com.group5.service.BooleanToJsonService;
import com.group5.service.LoginService;
import com.group5.service.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这个文件的boolean已经修改完了
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserssMapper userssMapper;
    @Autowired
    PlatformMapper platformMapper;
    LoginService loginService=new LoginService();
    BooleanToJsonService booleanToJsonService = new BooleanToJsonService();

    /***
     * 判断用户登录是否成功
     * @param username
     * @param password
     * @return 0代表密码错误，1代表登录成功，2表示用户名不存在
     */
    @GetMapping("/users")
    public BooleanToJsonService isLoginSuccess(@RequestParam String username, @RequestParam String password){
        List<Userss> usersses=userssMapper.selectByExample(loginService.selectUserByUserNameExample(username));
        if(!usersses.isEmpty()){
            if(Md5Util.Md5Generate(password,usersses.get(0).getSalt()).equals(usersses.get(0).getPassword()))
                booleanToJsonService.setCheck(true);
            else
                booleanToJsonService.setCheck(false);
        }
        else
            booleanToJsonService.setCheck(false);
        return booleanToJsonService;
    }


///***
// * 判断用户的username是否存在
// * 存在返回user类型，（uid&&question）
// * 不存在返回空
// * */
//    @GetMapping("/users")
//    public Userss isUserNameDuplication(@RequestParam String username){
//        List<Userss> usersses=userssMapper.selectByExample(loginService.selectUserByUserNameExample(username));
//        if(!usersses.isEmpty()){
//            Userss userss = new Userss();
//            userss.setUid(usersses.get(0).getUid());
//            userss.setQuestion(usersses.get(0).getQuestion());
//            return userss;
//        }
//        else
//            return null;
//}

/***
 * 此处前端给出一个json格式Userss对象,根据传过来的uid和答案与数据库内uid对应的答案是否匹配
 * 匹配返回1
 * 不匹配返回0
 */
    @GetMapping("/users/check")
    public BooleanToJsonService isAnswerMatch(@RequestParam String username,@RequestParam String answer){
        booleanToJsonService.setCheck(userssMapper.selectByExample(loginService.selectUserByUserNameExample(username)).get(0).getAnswer().equals(answer));
        return booleanToJsonService;
    }


    /***
     * 此处前端传过来一个json格式Userss对象，更新密码
     * 测试    "uid":10000018,
     *     "password":"12wsd"
     *     username:yby
     *     更新后数据库密文：7075f2b85f2d5742f5c355088f2f26de
     *     salt:138dfb49588a5e49
     * 返回1
     */
    @PostMapping("/users/update")
    public BooleanToJsonService updateAnswer(@RequestParam String username,@RequestParam String password){
        Userss userss=userssMapper.selectByExample(loginService.selectUserByUserNameExample(username)).get(0);
        loginService.MD5UserPasswordAndSalt(userss,password);
        booleanToJsonService.setCheck(1 == userssMapper.updateByPrimaryKeySelective(userss));
        return  booleanToJsonService;
    }




    /**
     * 判断平台登录是否成功
     * @param oname
     * @return 1代表登录成功，0代表用户名不存在，重新输入
     */

    @GetMapping("/platforms")
    public BooleanToJsonService isPlatformNameDuplication(@RequestParam String oname){
        List<Platform> platforms=platformMapper.selectByExample(loginService.selectPlatformByONameExample(oname));
        System.out.println(platforms);
        booleanToJsonService.setCheck(!platforms.isEmpty());
        return booleanToJsonService;
    }


    
    @GetMapping("/UserByUsername")
    public Userss getUserByUsername(@RequestParam String username){
        Userss userss=userssMapper.selectByExample(loginService.selectUserByUserNameExample(username)).get(0);
        userss.setAnswer(null);
        return userss;
    }


    @GetMapping("/isUserHasCreated")
    public BooleanToJsonService isUserHasCreated(@RequestParam String username){
        List<Userss> usersses= userssMapper.selectByExample(loginService.selectUserByUserNameExample(username));
        if(usersses.isEmpty())
            booleanToJsonService.setCheck(false);
        else booleanToJsonService.setCheck(true);
        return booleanToJsonService;
    }
}
